<?php
/**
 * Created by PhpStorm.
 * User: glq
 * Date: 2019/4/4
 * Time: 13:30
 */
namespace Yjsh\Controller;

use Common\Controller\AppframeController;
use Yjsh\Model\GroupBuyGroupModel;

// 拼团购买
class AdmingroupbuyController extends AppframeController {
    private $page_size;
    // 成团模板
    private $GroupBuyTempModel;
    // 团模板商品
    private $GroupBuyProductModel;
    // 团购团队表
    private $GroupBuyGroupModel;
    // 团队成员表
    private $GroupBuyUserModel;

    function _initialize() {
        parent::_initialize();
        $this->page_size                = 5;
        $this->GroupBuyTempModel        = D("Yjsh/GroupBuyTemp");
        $this->GroupBuyProductModel     = D("Yjsh/GroupBuyProduct");
        $this->GroupBuyGroupModel       = D("Yjsh/GroupBuyGroup");
        $this->GroupBuyUserModel        = D("Yjsh/GroupBuyUser");
    }


    // 获取团购的模板列表
    public function get_group_buy_temp_list() {
        $result = array('status' => 1);
        $count  = $this->GroupBuyTempModel->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = $this->GroupBuyTempModel
            ->limit($page->firstRow , $page->listRows)
            ->order('created_at desc')
            ->select();

        $result['lists']        = $lists;
        $result['total_count']  = $count;
        $result['total_pages']  = ceil($count / $this->page_size);
        $this->ajaxReturn($result);
    }

    // 获取团购模板的商品列表
    public function get_group_buy_pruduct_list()
    {
        $result = array();
        $gbt_id = I('get.gbt_id', 0);
        if ($gbt_id) {
            $count  = $this->GroupBuyProductModel->where(['gbt_id' => $gbt_id])->count();
            $page   = $this->page($count, $this->page_size);
            $lists  = $this->GroupBuyProductModel
                ->alias('gp')
                ->join("__PRODUCT__ p on gp.p_id = p.id")
                ->limit($page->firstRow , $page->listRows)
                ->order('sort desc, created_at desc')
                ->field("gp.*, p.name, p.sale_price, p.pre_pic, p.stock")
                ->select();

            foreach ($lists as $k => $v) {
                $today_sale_info    = $this->GroupBuyGroupModel->where(['gbp_id' => $v['id'], 'created_at' => ['GT', strtotime(date("Y-m-d"))]])->field('left_user_num, status')->select();
                $finish_num         = 0;
                $processing_num     = 0;
                foreach ($today_sale_info as $v1) {
                    if ($v1['left_user_num'] <= 0 && $v1['status'] == GroupBuyGroupModel::GROUP_BUY_GROUP_STATUS_SUCCESS) {
                        $finish_num++;
                    } elseif ($v1['status'] == GroupBuyGroupModel::GROUP_BUY_GROUP_STATUS_ING) {
                        $processing_num++;
                    }
                }
                $v['finish_num']    = $finish_num;
                $v['processing_num']= $processing_num;
                $lists[$k]          = $v;
            }

            $config                 = $this->get_config();
            $result['status']       = 1;
            $result['img_base_url'] = $config ? $config['img_base_url'] : "";
            $result['lists']        = $lists;
            $result['total_count']  = $count;
            $result['total_pages']  = ceil($count / $this->page_size);
        } else {
            $result['msg'] = '参数错误';
        }
        $this->ajaxReturn($result);
    }

    // 创建/更新团购模板商品信息
    public function edit_group_buy_product()
    {
        $gbp_id = I('get.gbp_id', 0);
        $result = array();
        if (IS_POST) {
            $gbp_info = I('post.gbp_info');
            if ($gbp_id) {
                $this->GroupBuyProductModel->where(['id' => $gbp_id])->save($gbp_info);
            } else {
                unset($gbp_info['id']);
                $gbp_info['created_at']         = time();
                $gbp_info['left_group_num']     = $gbp_info['total_group_num'];
                $this->GroupBuyProductModel->add($gbp_info);
            }
            $result['status']   = 1;
            $result['msg']      = '操作成功';
        } else {
            if ($gbp_id) {
                $gbp_info = $this->GroupBuyProductModel->where(['id' => $gbp_id])->find();
                if ($gbp_info) {
                    $result['status'] = 1;
                    // 删除该团购商品
                    $del = I('get.del', 0);
                    if ($del) {
                        $this->GroupBuyProductModel->where(['id' => $gbp_id])->delete();
                        $result['msg'] = '删除成功';
                    } else {
                        $result['gbp_info'] = $gbp_info;
                    }
                } else {
                    $result['msg'] = '参数错误';
                }
            }
        }
        $this->ajaxReturn($result);
    }

    // 更改商品排序
    public function change_group_product_sort() {
        $gbp_id = I('request.gbp_id');
        $result = array();
        if ($gbp_id && $info = $this->GroupBuyProductModel->where(['id' => $gbp_id])->find()) {
            $sort = I('request.sort');
            if ($sort != $info['sort']) {
                $this->GroupBuyProductModel->where(['id' => $gbp_id])->save(['sort' => $sort]);
                $result['status'] = 1;
            }
        } else {
            $result['msg'] = '参数错误';
        }
        $this->ajaxReturn($result);
    }

    private function get_config()
    {
        $info = M('config')->where(['id' => 2])->find();
        if (!$info) {
            writeFile("GroupBuy get config error!!!!");
        }
        return $info ?: [];
    }
}